package westh.ilib.struts.action.manage.resource;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.mule.extras.client.MuleClient;
import org.mule.umo.UMOMessage;

import westh.config.Config;
import westh.log.YyError;
import westh.util.YyAction;
import westh.util.YyID;

/**
 * @author zhangjian
 */
public class ResourceAddAction extends YyAction {
	
	private static Logger log = Logger.getLogger(ResourceAddAction.class);
	//get clouds Ip address
	String localhostUrl = Config.get("localhostUrl");
	String remoteUrl = Config.get("remoteUrl");

	public ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		westh.log.YyLog.addFootprint(request, "action－add resource");

		DynaActionForm f = (DynaActionForm) form;

		String rscTitle = f.getString("resTitle");
		String rscSummary = f.getString("resSummary");
		String rscPubDate = f.getString("resPubDate");
		String rscAuthor = f.getString("resAuthor");
		String rscOtherAuthor=f.getString("resOtherAuthor");
		String rscType = f.getString("resType");
		String rscField = f.getString("resField");
		String rscUrl = f.getString("resUrl");
		YyError.show("0001");
		log.debug(Config.getMsg("IDLIB_TEST"));
	  //get number
	  String strId = YyID.getUUID32();
  	/*************************cdoi info start******************************/
	  String rId = "012f717f8dde4129a95837bf153de5a0"+strId;
	  //形成json进行封装 
	  JSONObject jsonCdoi = new JSONObject(); 

	  jsonCdoi.put("WESTDO_HANDLE",rId);                                    //the unique identifier
	  jsonCdoi.put("CDOI_AGENCY_ID", "012f717f8dde4129a95837bf153de5a0");   //organization id 注册机构编码
	  jsonCdoi.put("DC_LANGUAGE", "c7d2ec860cee4b03ba845da0233ed977");      //language identifier
	  jsonCdoi.put("CDOI_CUSTOMER_ID", "f41825a60cf44e83b83cbd9114220f29"); //user id
	  jsonCdoi.put("DC_TYPE", "27278cfe6e1c429aa2197b3ff34a11ab");          //type id
	  jsonCdoi.put("DC_TITLE", rscTitle);                                   //title
	  jsonCdoi.put("DC_CREATOR", rscAuthor);                                //creator
	  jsonCdoi.put("DC_SUBJECT", "");                                       //subject
	  jsonCdoi.put("DC_DESCRIPTION", rscSummary);                           //description
	  jsonCdoi.put("DC_PUBLISHER", "");                                     //publisher
	  jsonCdoi.put("DC_CONTRIBUTOR", "");                                   //contributors
	  jsonCdoi.put("DC_DATE", rscPubDate);                                  //date
	  jsonCdoi.put("DC_FORMAT", "");                                        //format
	  jsonCdoi.put("DC_IDENTIFIER", "");                                    //identifier
	  jsonCdoi.put("DC_SOURCE", "");                                        //source
	  jsonCdoi.put("DC_RELATION", "");                                      //relation
	  jsonCdoi.put("DC_COVERAGE", "");                                      //coverage                        
	  jsonCdoi.put("DC_RIGHTS", "");                                        //visit right
	  jsonCdoi.put("CDOI_URL_SOURCE", "");                                  //resource url
	  jsonCdoi.put("CDOI_STATUS", "1");                                     //registration status, 2 for pass
	  jsonCdoi.put("CDOI_NOTE", "");                                        //remark
	  
	  String cdoiJson = jsonCdoi.toString();
	  String cdoiId="";
	  cdoiId=rId;
	  
/*	  try{
		  MuleClient clientW = new MuleClient();
//		  UMOMessage umo = clientW.send("axis:http://192.168.28.112:4281/Cdoi/cdoiCdoi?method=addJson",cdoiJson,null);
		  UMOMessage umo = clientW.send("axis:http://192.168.1.200:4281/Cdoi/cdoiCdoi?method=addJson",cdoiJson,null);
		  cdoiId = (String)umo.getPayload();
	  } catch (Exception e) {
		throw new RuntimeException(e);
	  }
*/		/** ****************************cdoi info end************************ */
	  
	  //modified by yanfei get authors cdoi 2010.08.14 begin
	  //get number
	  strId = YyID.getUUID32();
	  rId = "012f717f8dde4129a95837bf153de5a0"+strId;
	  
	  JSONObject firsAuthorjsonCdoi = new JSONObject(); 
	  jsonCdoi.put("WESTDO_HANDLE",rId);                                    //唯一标识

	  String firstAuthorcdoiJson = firsAuthorjsonCdoi.toString();
	  String firstAuthorcdoiId="";
	  firstAuthorcdoiId=rId;
/*	  try{
		  MuleClient clientW = new MuleClient();
//		  UMOMessage umo = clientW.send("axis:http://192.168.28.112:4281/Cdoi/cdoiCdoi?method=addJson",cdoiJson,null);
		  UMOMessage umo = clientW.send("axis:http://192.168.1.200:4281/Cdoi/cdoiCdoi?method=addJson",firstAuthorcdoiJson,null);
		  firstAuthorcdoiId = (String)umo.getPayload();
	  } catch (Exception e) {
		throw new RuntimeException(e);
	  }
*/	  
	  String [] authors = rscOtherAuthor.split("#");
	  int numAuthors = authors.length;
	  
	  String otherAuthorId="";
	  
	  for(int i=0;i<numAuthors;i++)
	  {
		  strId = YyID.getUUID32();
		  rId = "012f717f8dde4129a95837bf153de5a0"+strId;
		  
		  JSONObject authorjsonCdoi = new JSONObject(); 
		  jsonCdoi.put("WESTDO_HANDLE",rId);                                    //唯一标识
		  jsonCdoi.put("CDOI_AGENCY_ID", "012f717f8dde4129a95837bf153de5a0");   //注册机构编码
		  jsonCdoi.put("DC_LANGUAGE", "c7d2ec860cee4b03ba845da0233ed977");      //语种标识符
		  jsonCdoi.put("CDOI_CUSTOMER_ID", "f41825a60cf44e83b83cbd9114220f29"); //用户标识符
		  jsonCdoi.put("DC_TYPE", "27278cfe6e1c429aa2197b3ff34a11ab");          //类型标识符
		  jsonCdoi.put("DC_TITLE", authors[i]);                                   //题名
		  jsonCdoi.put("DC_CREATOR", authors[i]);                                //创建者
		  jsonCdoi.put("DC_SUBJECT", "");                                       //主题
		  jsonCdoi.put("DC_DESCRIPTION", "");                           //描述
		  jsonCdoi.put("DC_PUBLISHER", "");                                     //出版者
		  jsonCdoi.put("DC_CONTRIBUTOR", "");                                   //其他责任者
		  jsonCdoi.put("DC_DATE", "");                                  //日期
		  jsonCdoi.put("DC_FORMAT", "");                                        //格式
		  jsonCdoi.put("DC_IDENTIFIER", "");                                    //标识符
		  jsonCdoi.put("DC_SOURCE", "");                                        //来源
		  jsonCdoi.put("DC_RELATION", "");                                      //关联
		  jsonCdoi.put("DC_COVERAGE", "");                                      //覆盖范围                         
		  jsonCdoi.put("DC_RIGHTS", "");                                        //权限
		  jsonCdoi.put("CDOI_URL_SOURCE", "");                                  //资源URL
		  jsonCdoi.put("CDOI_STATUS", "1");                                     //资源注册状态，2审核通过
		  jsonCdoi.put("CDOI_NOTE", "");                                        //备注
		  
		  String authorcdoiJson = authorjsonCdoi.toString();
		  String authorcdoiId="";
		  authorcdoiId=rId;
/*		  try{
			  MuleClient clientW = new MuleClient();
//			  UMOMessage umo = clientW.send("axis:http://192.168.28.112:4281/Cdoi/cdoiCdoi?method=addJson",cdoiJson,null);
			  UMOMessage umo = clientW.send("axis:http://192.168.1.200:4281/Cdoi/cdoiCdoi?method=addJson",authorcdoiJson,null);
			  authorcdoiId = (String)umo.getPayload();
		  } catch (Exception e) {
			throw new RuntimeException(e);
		  }
*/		  
		  otherAuthorId=otherAuthorId+authorcdoiId+"#";
	  }
	  
	  
	  
	  
	  //modified by yanfei 2010.08.14 end
	  

		/** ****************************cdo info start************************ */
		JSONObject jsonObj = new JSONObject();

		
		jsonObj.put("rscId", cdoiId);                        // resource cdoi
		jsonObj.put("rscTitle", rscTitle);                  // resource title
		jsonObj.put("rscFirstAuthorId", firstAuthorcdoiId);              // resource first author
		jsonObj.put("rscOtherAuthorsId", otherAuthorId);	//resource other author
		jsonObj.put("rscCoverage", "0");						//resource coverage
		jsonObj.put("rscCreator", "0");						//resource creator
		jsonObj.put("rscPublishDate", rscPubDate);			//resource publish date
		jsonObj.put("rscAbstract", rscSummary);				//resource abstract
		jsonObj.put("rscFormat", "0");     					//resource format
		jsonObj.put("rscLanguage", "0");     				//resource language
		jsonObj.put("rscPublisher", "0");     				//resource publisher
		jsonObj.put("rscRelation", "0");     				//resource relation
		jsonObj.put("rscRights", "0");  						//resource visit right
		jsonObj.put("rscSource", "0");  						//resource source (which conference, or which journal)
		jsonObj.put("rscSubject", "0");  					//resource subject
		jsonObj.put("rscType", rscType);                    //resource type
		jsonObj.put("rscField", rscField);                  //resource field
		jsonObj.put("rscUrl", rscUrl);                      //resource url
		String input = jsonObj.toString();
		
		log.debug("input="+input);

		String id = "";
		try {
			MuleClient clientD = new MuleClient();
			UMOMessage umo = clientD.send("axis:http://"+localhostUrl+":4381/LIBJson/iDlibJson?method=addResJson",input, null);
			id = (String) umo.getPayload();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		/** ****************************cdo info end************************ */
		if (id.equals("1")) {
			String insertSubjectIndustry = "Add resource success! <br/>";
			request.setAttribute("info", insertSubjectIndustry);
			request.setAttribute("redirect", "../resAddPre.do");
			return mapping.findForward("info");
		} else {
			String insertSubjectIndustry = "Add resource failure! <br/>";
			request.setAttribute("info", insertSubjectIndustry);
			request.setAttribute("redirect", "../resAddPre.do");
			return mapping.findForward("info");
		}
	}
}